import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from page.base_page import BasePage


class DisbursementAccount(BasePage):
    __platform_account = By.XPATH, "//*[text()='平台虚户']"
    __platform_account_detail = By.XPATH, '//*[@href="/financial/account/account/index"]'
    __order_management = By.XPATH, "//*[text()='订单管理']"
    __proxy_order = By.XPATH, "//*[text()='代理订单']"
    __proxy_order_list = By.XPATH, "//*[@href = '/order/proxyOrder/proxyOrderApply']"

    def goto_platform_account_detail(self):
        from page.platform_account_detail_page import PlatformAccountDetail
        with allure.step('点击平台虚户'):
            self.find_and_click(self.__platform_account)
        with allure.step('点击账户详情'):
            self.find_and_click(self.__platform_account_detail)
        return PlatformAccountDetail(self.driver)

    def goto_proxy_order(self):
        from page.proxy_order_apply_page import ProxyOrderApply
        with allure.step('点击订单管理'):
            self.find_and_click(self.__order_management)
        with allure.step('点击代理订单'):
            self.find_and_click(self.__proxy_order)
        with allure.step('点击订单列表'):
            self.find_and_click(self.__proxy_order_list)
            self.wait(EC.visibility_of_element_located((By.XPATH, "//*[text()='借贷人姓名']")), time_out=10)
        return ProxyOrderApply(self.driver)

    def get_loan_amount_available(self, product_name):
        with allure.step(f"获取{product_name}产品的放款可贷金额"):
            time.sleep(0.5)
            while True:
                try:
                    loan_amount_available = self.find(By.XPATH, f"//*[text()='{product_name}']/../../td[7]/div").text
                    break
                except:
                    pass
        return loan_amount_available

    def get_in_transit_balance(self, product_name):
        with allure.step(f"获取{product_name}产品的放款在途金额"):
            in_transit_balance = self.find(By.XPATH, f"//*[text()='{product_name}']/../../td[8]/div").text
        return in_transit_balance

    def get_freeze_amount(self, product_name):
        with allure.step(f"获取{product_name}产品的冻结金额"):
            freeze_amount = self.find(By.XPATH, f"//*[text()='{product_name}']/../../td[9]/div").text
        return freeze_amount
